import vv from "./vv";
import v from "./v";
import GameData from "./GameData";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

   

    
    //汽车影子
    @property(cc.Node)
    shaicheCaryingzi:cc.Node = null

    

    //汽车数组
    @property(cc.Node)
    shaichecarspar:cc.Node = null

 

    //速度值
    @property(cc.Label)
    shaichesuduzhi:cc.Label

    //进度条panel
    @property(cc.Node)
    shaichejindutiaoPanel:cc.Node = null


    //金币
    @property(cc.Label)
    gold:cc.Label = null
   
    // onLoad () {}


    //当前选择的车
    @property(cc.Node)
    shaicheOnCar:cc.Node = null



    LocalCarsArray = ['叉车','坦克','大脚怪','小橘牛','小红','德邦电动车','挖土机','救护车','救火车','橘色雄鹿','申通电动车','白色幽灵','葱花面包','铲车','顺丰小黑']

    start () {
       

      
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
       v.on('goldchange',()=>
       {
        let luogang = 
        {
            "luogang":"哈哈",
            "luogang1":"哈哈",
            "luogang2":"哈哈",
            "luogang3":"哈哈",
            "luogang4":"哈哈",
            "luogang5":"哈哈"
        }
           if(this.gold!=null)
           {
            this.gold.string = GameData.Instance.goldNum.toString() 
           }
       })
    }

    onEnable()
    {
        console.log('当前拥有的车辆',GameData.Instance.carsArray)
        if(GameData.Instance.carsArray==undefined)
        {
            let luogang = 
            {
                "luogang":"哈哈",
                "luogang1":"哈哈",
                "luogang2":"哈哈",
                "luogang3":"哈哈",
                "luogang4":"哈哈",
                "luogang5":"哈哈"
            }
            GameData.Instance.carsArray = ['小红','橘色雄鹿'] 
        }
        this.shaicheOnCar.getComponent(cc.Animation).play()
        this.shaicheCaryingzi.getComponent(cc.Animation).play()
        this.gold.string = GameData.Instance.goldNum.toString()
        let url = '汽车/'+GameData.Instance.Onshaichecar+'.png'
        vv.SetImageByLocalUrl(url,this.shaicheCaryingzi)
        vv.SetImageByLocalUrl(url,this.shaicheOnCar)
        for(let i = 0;i<GameData.Instance.carsArray.length;i++)
        {
            for(let j = 0;j<this.shaichecarspar.childrenCount;j++)
            {
                    if(GameData.Instance.carsArray[i]==this.shaichecarspar.children[j].name)
                    {
                        let luogang = 
                        {
                            "luogang":"哈哈",
                            "luogang1":"哈哈",
                            "luogang2":"哈哈",
                            "luogang3":"哈哈",
                            "luogang4":"哈哈",
                            "luogang5":"哈哈"
                        }
                        this.shaichecarspar.children[j].getChildByName('car').active = true
                        this.shaichecarspar.children[j].getChildByName('name').getComponent(cc.Label).string = this.shaichecarspar.children[j].name
                        this.shaichecarspar.children[j].getChildByName('car').color = new cc.Color(65,57,57)
                        this.shaichecarspar.children[j].getChildByName('yingzi').opacity = 255
                        this.shaichecarspar.children[j].getChildByName('buy').active = false
                        if(GameData.Instance.Onshaichecar == this.shaichecarspar.children[j].name)
                        {
                            this.shaichecarspar.children[j].getChildByName('xuanze').active = false
                            this.shaichecarspar.children[j].getChildByName('shou').active = true 
                            this.shaichecarspar.children[j].getChildByName('car').color = new cc.Color(255,255,255)
                            this.shaichecarspar.children[j].getChildByName('yingzi').opacity = 204
                          
                        }
                        else
                        {
                            this.shaichecarspar.children[j].getChildByName('shou').active = false
                            this.shaichecarspar.children[j].getChildByName('xuanze').active = true 
                        }  
                   
                    }
                        this.shaichecarspar.children[j].getChildByName('xuanze').children[0].on('touchend',()=>
                        {
                            let luogang = 
                            {
                                "luogang":"哈哈",
                                "luogang1":"哈哈",
                                "luogang2":"哈哈",
                                "luogang3":"哈哈",
                                "luogang4":"哈哈",
                                "luogang5":"哈哈"
                            }
                            if(this.shaichecarspar.children[j].getChildByName('car').active == true&&this.shaichecarspar.children[j].getChildByName('shou').active == false)
                            {
                                if(GameData.Instance.shaichevoiceOnOrOff == true)
                                {
                                    cc.find('Canvas/AudioManager').children[6].getComponent(cc.AudioSource).play()
                                }
                                console.log('选择',this.shaichecarspar.children[j].name)
                                this.shaichecarspar.children[j].getChildByName('shou').active = true
                                this.shaichecarspar.children[j].getChildByName('xuanze').active = false
                                this.shaichecarspar.children[j].getChildByName('buy').active = false
                                this.shaichecarspar.children[j].getChildByName('car').color = new cc.Color(255,255,255)
                                this.shaichecarspar.children[j].getChildByName('yingzi').opacity = 204
                                let url = '汽车/'+this.shaichecarspar.children[j].name+'.png'
                                GameData.Instance.Onshaichecar =this.shaichecarspar.children[j].name

                                if(GameData.Instance.Onshaichecar == '葱花面包'||GameData.Instance.Onshaichecar == '橘色雄鹿'||GameData.Instance.Onshaichecar == '叉车')
                                {
                                    for(let i = 0;i<this.shaichecarspar.childrenCount;i++)
                                    {
                                        this.shaichejindutiaoPanel.children[i].active = true
                                    }
                                    this.shaichesuduzhi.string = '800'
                                    this.shaichejindutiaoPanel.children[0].active = false
                                    this.shaichejindutiaoPanel.children[1].active = false
                                    this.shaichejindutiaoPanel.children[2].active = false
                                }
                                if(GameData.Instance.Onshaichecar == '小橘牛'||GameData.Instance.Onshaichecar == '救火车'||GameData.Instance.Onshaichecar == '黄卡'||GameData.Instance.Onshaichecar == '韵达电动车')
                                {
                                    let luogang = 
                                    {
                                        "luogang":"哈哈",
                                        "luogang1":"哈哈",
                                        "luogang2":"哈哈",
                                        "luogang3":"哈哈",
                                        "luogang4":"哈哈",
                                        "luogang5":"哈哈"
                                    }
                                    for(let i = 0;i<this.shaichecarspar.childrenCount;i++)
                                    {
                                        this.shaichejindutiaoPanel.children[i].active = true
                                    }
                                    this.shaichesuduzhi.string = '900'
                                    this.shaichejindutiaoPanel.children[0].active = false
                                    this.shaichejindutiaoPanel.children[1].active = false
                                }
                    
                                if(GameData.Instance.Onshaichecar == '白色幽灵'||GameData.Instance.Onshaichecar == '大脚怪'||GameData.Instance.Onshaichecar == '铲车'||GameData.Instance.Onshaichecar == '韵达电动车')
                                {
                                    for(let i = 0;i<this.shaichecarspar.childrenCount;i++)
                                    {
                                        this.shaichejindutiaoPanel.children[i].active = true
                                    }
                                    this.shaichesuduzhi.string = '1000'
                                    this.shaichejindutiaoPanel.children[0].active = false
                                }
                    
                                if(GameData.Instance.Onshaichecar == '黑影23号'||GameData.Instance.Onshaichecar == '绿影')
                                {
                                    for(let i = 0;i<this.shaichecarspar.childrenCount;i++)
                                    {
                                        this.shaichejindutiaoPanel.children[i].active = true
                                    }
                                    this.shaichesuduzhi.string = '1100'
                                }
                                v.emit('shaichecarChange')
                                console.log('url',url)
                                vv.SetImageByLocalUrl(url,this.shaicheOnCar)
                                vv.SetImageByLocalUrl(url,this.shaicheCaryingzi)
                                for(let m = 0;m<this.shaichecarspar.childrenCount;m++)
                                {
                                    if(this.shaichecarspar.children[m].name !=this.shaichecarspar.children[j].name)
                                    {
                                        this.shaichecarspar.children[m].getChildByName('shou').active = false
                                        this.shaichecarspar.children[m].getChildByName('xuanze').active = true 
                                        this.shaichecarspar.children[m].getChildByName('car').color = new cc.Color(65,57,57)
                                        this.shaichecarspar.children[j].getChildByName('yingzi').opacity = 255
                                    }
                                }
                            }
                        })

                        this.shaichecarspar.children[j].getChildByName('buy').children[0].on('touchend',()=>
                        {
                            let luogang = 
                            {
                                "luogang":"哈哈",
                                "luogang1":"哈哈",
                                "luogang2":"哈哈",
                                "luogang3":"哈哈",
                                "luogang4":"哈哈",
                                "luogang5":"哈哈"
                            }
                            if(GameData.Instance.goldNum<(Number)(this.shaichecarspar.children[j].getChildByName('buy').children[1].getComponent(cc.Label).string))
                            {
                                vv.showTip('金币不足')
                            }
                            else if(this.shaichecarspar.children[j].getChildByName('car').active == false)
                            {
                                console.log('购买成功')
                                GameData.Instance.goldNum-=(Number)(this.shaichecarspar.children[j].getChildByName('buy').children[1].getComponent(cc.Label).string)
                                v.emit('goldchange')
                                vv.showTip('购买成功！')
                                this.shaichecarspar.children[j].getChildByName('name').getComponent(cc.Label).string = this.shaichecarspar.children[j].name
                                this.shaichecarspar.children[j].getChildByName('car').active = true
                                this.shaichecarspar.children[j].getChildByName('buy').active = false
                                this.shaichecarspar.children[j].getChildByName('car').color = new cc.Color(65,57,57)
                                this.shaichecarspar.children[j].getChildByName('yingzi').opacity = 255
                                this.shaichecarspar.children[j].getChildByName('xuanze').active = true 
                                if(this.shaichecarspar.children[j].getChildByName('car').children[1]!=null)
                                
                                {
                                    this.shaichecarspar.children[j].getChildByName('car').children[1].getComponent(cc.ParticleSystem).resetSystem()
                                }
                               
                                this.shaichecarspar.children[j].getChildByName('xuanze').children[0].on('touchend',()=>
                                {
                                    if( this.shaichecarspar.children[j].getChildByName('shou').active == false)
                                    {
                                        console.log('选择',this.shaichecarspar.children[j].name)
                                        this.shaichecarspar.children[j].getChildByName('shou').active = true
                                        this.shaichecarspar.children[j].getChildByName('xuanze').active = false
                                        this.shaichecarspar.children[j].getChildByName('buy').active = false
                                        this.shaichecarspar.children[j].getChildByName('car').color = new cc.Color(255,255,255)
                                        this.shaichecarspar.children[j].getChildByName('yingzi').opacity = 204
                                        let url = '汽车/'+this.shaichecarspar.children[j].name+'.png'
                                        GameData.Instance.Onshaichecar =this.shaichecarspar.children[j].name
                                        console.log('url',url)
                                        vv.SetImageByLocalUrl(url,this.shaicheOnCar)
                                        vv.SetImageByLocalUrl(url,this.shaicheCaryingzi)
                                        for(let m = 0;m<this.shaichecarspar.childrenCount;m++)
                                        {
                                            if(this.shaichecarspar.children[m].name !=this.shaichecarspar.children[j].name)
                                            {
                                                this.shaichecarspar.children[m].getChildByName('shou').active = false
                                                this.shaichecarspar.children[m].getChildByName('xuanze').active = true 
                                                this.shaichecarspar.children[j].getChildByName('car').color = new cc.Color(65,57,57)
                                                this.shaichecarspar.children[j].getChildByName('yingzi').opacity = 255
                                            }
                                        } 
                                    }
                                  
                                })
                            }
                        })
            }
                   
                   
        }
    }

    //关闭车库按钮
    clouseCheku()
    {
        if(GameData.Instance.shaichevoiceOnOrOff == true)
        {
            cc.find('Canvas/AudioManager').children[5].getComponent(cc.AudioSource).play()
        }
        this.node.active = false
    }


    //随机按钮
    suijiBtn()
    {
        if(GameData.Instance.shaichevoiceOnOrOff == true)
        {
            cc.find('Canvas/AudioManager').children[5].getComponent(cc.AudioSource).play()
        }
    }


    // update (dt) {}
}
